/* 
 * File:   ListadoNiveles.cpp
 * Author: agu
 * 
 * Created on 7 de junio de 2014, 16:41
 */

#include "ListadoNiveles.h"
#include <iostream>

ListadoNiveles::ListadoNiveles()
: m_VBox(Gtk::ORIENTATION_VERTICAL),
m_Button_Back("Volver") {
    set_title("Listado de campañas");
    set_border_width(5);
    set_default_size(400, 600);
    set_position(Gtk::WIN_POS_CENTER_ALWAYS);
    add(m_VBox);

    //Add the TreeView, inside a ScrolledWindow, with the button underneath:
    m_ScrolledWindow.add(m_TreeView);

    //Only show the scrollbars when they are necessary:
    m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

    m_VBox.pack_start(m_ScrolledWindow);
    m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);

    m_ButtonBox.pack_start(m_Button_Back, Gtk::PACK_SHRINK);
    m_ButtonBox.set_border_width(5);
    m_ButtonBox.set_layout(Gtk::BUTTONBOX_CENTER);

    m_Button_Back.signal_clicked().connect(sigc::mem_fun(*this,
            &ListadoNiveles::back));

    //Create the Tree model:
    m_refTreeModel = Gtk::ListStore::create(m_Columns);
    m_TreeView.set_model(m_refTreeModel);
    loadRows();
    addColumns();


    //Make all the columns reorderable:
    //This is not necessary, but it's nice to show the feature.
    //You can use TreeView::set_column_drag_function() to more
    //finely control column drag and drop.
    //    for (guint i = 0; i < 2; i++) {
    //        Gtk::TreeView::Column* pColumn = m_TreeView.get_column(i);
    //        pColumn->set_reorderable();
    //    }
    m_TreeView.signal_row_activated().connect(sigc::mem_fun(*this,
            &ListadoNiveles::select));
    this->signal_hide().connect(sigc::mem_fun(this, &ListadoNiveles::close));
    show_all_children();

}

ListadoNiveles::~ListadoNiveles() {
}

void ListadoNiveles::loadRows() {
    //Fill the TreeView's model
    Gtk::TreeModel::Row row = *(m_refTreeModel->append());
    row[m_Columns.m_col_id] = 1;
    row[m_Columns.m_col_name] = "Billy Bob";

    row = *(m_refTreeModel->append());
    row[m_Columns.m_col_id] = 2;
    row[m_Columns.m_col_name] = "Joey Jojo";

    row = *(m_refTreeModel->append());
    row[m_Columns.m_col_id] = 3;
    row[m_Columns.m_col_name] = "Rob McRoberts";


}

void ListadoNiveles::addColumns() {
    //Add the TreeView's view columns:
    //This number will be shown with the default numeric formatting.
    m_TreeView.append_column("ID", m_Columns.m_col_id);
    m_TreeView.append_column("Name", m_Columns.m_col_name);

}

void ListadoNiveles::setWindowsBack(Gtk::Window *windowBack) {
    this->windowsBack = windowBack;
}

void ListadoNiveles::run() {
    this->show();
}

void ListadoNiveles::close() {
    this->hide();
}

void ListadoNiveles::back() {
    this->set_visible(false);
    this->windowsBack->set_visible(true);
}

void ListadoNiveles::select(const Gtk::TreeModel::Path& path,
        Gtk::TreeViewColumn* /* column */) {
    Gtk::TreeModel::iterator iter = m_refTreeModel->get_iter(path);
    if (iter) {
        Gtk::TreeModel::Row row = *iter;
        std::cout << "Row activated: ID=" << row[m_Columns.m_col_id] << ", Name="
                << row[m_Columns.m_col_name] << std::endl;
        this->set_visible(false);
        nivel.setWindowsBack(this);
        nivel.run();

        //        vNivel.run();
        //        levels.push_back(VistaNivel());
        //        this->set_visible(false);
        //        levels.at(0).setWindowsBack(this);
        //        levels.at(0).run();
        //        for (int i = 0; i < levels.size(); i++) {
        //            VistaNivel nivel = levels.at(i);
        //            if (nivel.getId() == row[m_Columns.m_col_id]) {
        //                //Editar el nivel con el id seleccionado
        //                nivel.run();
        //            }
        //
        //        }

    }
}